<?php
/**
 * @file phoneinfo.php $
 * @author LiangJian (liangjian@haibao.com) $
 * @date 2015年7月20日 下午4:49:50 $
 * @brief APP接口请求历史记录 $
 */
namespace haibao\app\business;

class PhoneInfo extends \haibao\app\business\Base{
    
    /**
     * 保存设备信息、启动记录、得token记录、用户登录记录
     * @param array $dataArr
     */
    public static function addAppData($dataArr){
        if(!isset($dataArr['imei'])){
            throw new BusinessException('设备唯一ID不能为空', self::DATA_ERROR_CODE_EMPTY);
        }
        
        $data = new \haibao\app\data\PhoneInfo();
        
        $phoneInfoModel = $data->getPhoneInfoByImei($dataArr['imei']);
        if(empty($phoneInfoModel)){
        	//添加设备信息
        	$phoneInfoModel = new \haibao\app\model\data\PhoneInfo();
        	$phoneInfoModel->IMEI = $dataArr['imei'];
        	$phoneInfoModel->Model = $dataArr['model'];
        	$phoneInfoModel->OSType = $dataArr['system'];
        	$phoneInfoModel->OSVersion = $dataArr['systemVersion'];
        	$phoneInfoModel->CreateTime = new \DateTime();
        	$isIdFa = $phoneInfoModel::IS_IOS_IDFA_NO;
        	if(!empty($dataArr['isIdfa'])){
        		$isIdFa = $phoneInfoModel::IS_IOS_IDFA_YES;
        	}
        	$phoneInfoModel->IsIdFa = $isIdFa;
        	$data->addPhoneInfo($phoneInfoModel);
        }
        
        //记录开启应用记录
        $bootInfoModel = new \haibao\app\model\data\AppBootInfo();
        $bootInfoModel->IMEI = $dataArr['imei'];
        $bootInfoModel->OSType = $dataArr['system'];
        $bootInfoModel->OSVersion = $dataArr['systemVersion'];
        $bootInfoModel->Network = $dataArr['network'];
        $bootInfoModel->AppVersion = $dataArr['appVersion'];
        $bootInfoModel->CreateTime = new \DateTime();
        $data->addAppBootInfo($bootInfoModel);
        
        //添加接口返回Token记录
        $tokenModel = new \haibao\app\model\data\TokenInfo();
        $tokenModel->IMEI = $dataArr['imei'];
        $tokenModel->Token = $dataArr['token'];
        $tokenModel->AppVersion = $dataArr['appVersion'];
        $tokenModel->CreateTime = new \DateTime();
        $data->addTokenInfo($tokenModel);
        
        //添加用户登录记录
        /* if(!empty($dataArr['userId'])){
        	$userLoginHistoryModel = new \haibao\app\model\data\UserLoginHistory();
        	$userLoginHistoryModel->IMEI = $dataArr['imei'];
        	$userLoginHistoryModel->UserId = $dataArr['userId'];
        	$userLoginHistoryModel->CreateTime = new \DateTime();
        	$userLoginHistoryModel->Source = $userLoginHistoryModel::USER_TYPE_LOGIN;
        	$data->addUserLoginHistory($userLoginHistoryModel);
        } */
    }
    
    public static function getAppVersionByToken($token){
        if(!$token){
            return false;
        }
        $data = new \haibao\app\data\PhoneInfo();
        return $data->getAppVersionByToken($token);
    }
}
